﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DefeatEpidemic.Common;
using DefeatEpidemic.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DefeatEpidemic.API
{
    [Route("api/[controller]/[action]")]
    [GlobalActionFilter]
    public class CommunityController : BaseController
    {
        private readonly DefeatEpidemicContext _context;

        public CommunityController(DefeatEpidemicContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 获取小区列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>        
        [HttpGet]
        public ActionResult GetCommunityList(string name="", int pageindex=1,int pagesize=10)
        {
            Response resp = new Response();
            try
            {
                int skip = (pageindex - 1) * pagesize;
                
                var communities = _context.Community.AsQueryable()                    
                    .Select(u => new { code = u.labelNumber, u.name, u.province, u.city, u.area, u.status, u.detail, u.community_name });
                if (string.IsNullOrEmpty(name))
                {
                    communities.Where(u => u.community_name.Contains(name));
                }
                resp.code = 1;
                resp.data = new { total = communities.Count(), items = communities.Skip(skip).Take(pagesize) };
            }
            catch(Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message}.{ex.StackTrace}";
            }
            return Json(resp);
        }
        

        /// <summary>
        /// 获取小区二维码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCommunityQrCode(string code)
        {
            Response resp = new Response() { code = 1 };
            try
            {
                //var community = _context.Community.Where(u => u.labelNumber == code).FirstOrDefault();
                string url = Common.WinxinHelper.GetWxShareCode(code, "", 200, code + ".jpg");
                resp.data = new { url };
            }
            catch(Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message}.{ex.StackTrace}";
            }
            return Json(resp);
        }

        /// <summary>
        /// 获取小区信息
        /// </summary>
        /// <param name="code">小区识别码</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCommunitInfo(string code)
        {
            Response resp = new Response();
            try
            {
                var community = _context.Community.Where(u => u.labelNumber == code).FirstOrDefault();
                if (community != null)
                {
                    resp.code = 1;
                    resp.data = new { community };
                }                
            }
            catch (Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message}.{ex.StackTrace}";
            }
            return Json(resp);
        }
    }
}
